SuperStrict

Framework BRL.StandardIO

Import BRL.Random

Import Otus.Actor

Function Message:Object(data:Object)
	Local messages:String[]
	Repeat
		messages :+ [Rand(1,3)+":Message "+String(data)]
	Until Rand(0,1)
	Return messages
End Function

Function Deliver:Object(data:Object)
	For Local m:String = EachIn String[](data)
		reader[Int(m[0..1])-1].Send m
	Next
End Function

Function Read:Object(data:Object)
	Print String(data)
End Function

SeedRnd MilliSecs()

Print "Initializing actors..."

Global messager:TActor = CreateActor(Message)

Global mailman:TActor = CreateActor(Deliver)

Global reader:TActor[] = [CreateActor(Read), CreateActor(Read), CreateActor(Read)]

Print "Initializing messages..."

messager.Send("Hello World!").Chain(mailman)
messager.Send("Merry Christmas!").Chain(mailman)
messager.Send("Happy New Year!").Chain(mailman)

Print "Looping..."

Local done%
Repeat
	done = messager.Process()
	done :+ mailman.Process()
	done :+ reader[0].Process() 
	done :+ reader[1].Process() 
	done :+ reader[2].Process() 
Until done=0

Print "All done."